package com.example.slideshow;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;


public class Slideshow extends RelativeLayout{
	
	private String[] photolist;
	private ViewFlipper Container;
	private Context context;
	private GestureDetector gestureDetector;
	private Animation ContainerLeftIn;
	private Animation ContainerLeftOut;
	private Animation ContainerRightIn;
	private Animation ContainerRightOut;
	private LinearLayout bar;
	private TextView num_down;
	private Integer num;
	
	public Slideshow(Context context) {
		super(context);
		this.context = context;
		num = 1;
	}
	
	public Slideshow(Context ctx, AttributeSet attrs) {
	    super(ctx, attrs);
	    this.context = ctx;
	    num = 1;
    }

	public Void setSlideshow(String[] photolist){
		this.photolist = photolist;
		this.Container = new ViewFlipper(context);
		Container.setBackgroundColor(Color.WHITE);
		this.addView(Container, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		this.bar =  (LinearLayout) LayoutInflater.from(context).inflate(com.example.slideshow.R.layout.thebar, null);
		RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lay.addRule(RelativeLayout.CENTER_HORIZONTAL);
		num_down = (TextView) bar.findViewById(R.id.num_down);
		num_down.setText(num.toString()+"/"+photolist.length);
		this.addView(bar,lay);
		new downphoto(this.photolist).execute();
		gestureDetector = new GestureDetector(context,new MyGestureDetector());
		ContainerLeftIn = AnimationUtils.loadAnimation(context, com.example.slideshow.R.anim.slide_left_in);
        ContainerLeftOut = AnimationUtils.loadAnimation(context, com.example.slideshow.R.anim.slide_left_out);
        ContainerRightIn = AnimationUtils.loadAnimation(context, com.example.slideshow.R.anim.slide_right_in);
        ContainerRightOut = AnimationUtils.loadAnimation(context, com.example.slideshow.R.anim.slide_right_out);
		return null;
	}
	
	class MyGestureDetector extends SimpleOnGestureListener {

	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2,
	            float velocityX, float velocityY) {

	        // right to left
	        if(e1.getX() - e2.getX() > 10 && Math.abs(velocityX) > 20) {
	            //Log.i("onFling", "right to left");
	        	Container.stopFlipping();
	        	Container.setInAnimation(ContainerLeftIn);
	        	Container.setOutAnimation(ContainerLeftOut);
	        	Container.showNext();
	        	Container.startFlipping();
	            return false;

	        // left to right
	        }  else if (e2.getX() - e1.getX() > 10 && Math.abs(velocityX) > 20) {
	            //Log.i("onFling", "left to right");
	        	Container.stopFlipping();
	        	Container.setInAnimation(ContainerRightIn);
	        	Container.setOutAnimation(ContainerRightOut);
	        	Container.showPrevious();
	        	Container.setInAnimation(ContainerLeftIn);
	        	Container.setOutAnimation(ContainerLeftOut);
	        	Container.startFlipping();
	            return false; 
	        }

	        return false;
	    }

	}
	
	public class downphoto extends AsyncTask<Void, Integer, Void> {
		String[] foto;
		ImageView[] imageview;
		Bitmap[] images;
		public downphoto(String[] foto){
			this.foto = foto; 
			imageview = new ImageView[foto.length];
			images = new Bitmap[foto.length];
		}
		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			for (Integer i=0; i<foto.length; i++){
				imageview[i] = new ImageView(context);
				try {
					images[i] = BitmapFactory.decodeStream((InputStream)new URL(foto[i]).getContent());
					num++;
					publishProgress(i);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Container.startFlipping();
	    	Container.setInAnimation(ContainerLeftIn);
	    	Container.setOutAnimation(ContainerLeftOut);
	    	Container.setBackgroundColor(Color.TRANSPARENT);
	    	Container.setFlipInterval(4000);
			Container.setOnTouchListener(new View.OnTouchListener() {

		        public boolean onTouch(View v, MotionEvent event) {
		            if (gestureDetector.onTouchEvent(event)) {
		                return false;
		            }
		            return true;
		        }
			});
			bar.setVisibility(GONE);
			
		}
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			imageview[values[0]].setImageBitmap(images[values[0]]);
			imageview[values[0]].setScaleType(ImageView.ScaleType.FIT_XY);
			Container.addView(imageview[values[0]], new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			num_down.setText(num.toString()+"/"+photolist.length);
		}
		
	}
	
	
}
